package com.immunology.controller;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.immunology.dao.Syndrome;
import com.immunology.database.ConnectionManager;

public class SyndromeController implements Serializable {

	private static final long serialVersionUID = 1L;

	public List<Syndrome> getSyndromeFromDatabase() throws SQLException {
		List<Syndrome> syndromeList = new ArrayList<Syndrome>();

		ConnectionManager connectionManager = new ConnectionManager();
		Connection connection = connectionManager.getConnection();

		Statement statement = null;
		ResultSet resultSet = null;
		try {
			statement = connection.createStatement();
			try {
				String sql = "SELECT * FROM syndrome ";
				resultSet = statement.executeQuery(sql);
			} catch (Exception e) {
				System.out.println("Getting objects error");
			}
		} catch (Exception e) {
			System.out.println("Creating statement error");
		}

		while (resultSet.next()) {
			Syndrome currentSyndrome = new Syndrome();
			int syndromeId = resultSet.getInt(1);
			String syndromeName = resultSet.getString(2);
			currentSyndrome.setId(syndromeId);
			currentSyndrome.setName(syndromeName);
			syndromeList.add(currentSyndrome);
		}
		statement.close();
		resultSet.close();

		if (connectionManager != null)
			connectionManager.closeConnection(connection);
		return syndromeList;
	}

}
